package com.itheima.service.Impl;

import com.itheima.controller.Code;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务处理层
 */
@Service
public class BookServiceImpl implements BookService {

    /**
     * 业务层测试用Junit
     * 表现层测试用postman
     */
    @Autowired
    private BookDao bookDao;

    /**
     * 保存图书信息
     */
    public boolean save(Book book) {    //dao操作影响了几行来判断成功与否
        return bookDao.save(book) > 0 ;
    }

    /**
     * 修改图书信息
     */
    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    /**
     * 删除图书信息
     */
    public boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }

    /**
     * 根据ID查找图书信息
     */
    public Book getById(Integer id) {

        if(id == 1) {
            throw new BusinessException(Code.BUSINESS_ERR, "请不要用你的技术挑战我的耐心！！！");
        }

        //将可能出现的异常用刚定义的异常类来包装
        try {
            int i = 1/0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时，请重试！！！",e);  //将异常包装
        }
        return bookDao.getById(id);
    }

    /**
     * 查询所有图书信息
     */
    public List<Book> getAll() {
        return bookDao.getAll();
    }

}
